package com.wing.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wing.constant.MessageConstant;
import com.wing.constant.StatusConstant;
import com.wing.dto.DishDTO;
import com.wing.dto.DishPageQueryDTO;
import com.wing.entity.Dish;
import com.wing.entity.DishFlavor;
import com.wing.exception.DeletionNotAllowedException;
import com.wing.mapper.DishFlavorMapper;
import com.wing.mapper.DishMapper;
import com.wing.mapper.SetMealDishMapper;
import com.wing.result.PageResult;
import com.wing.service.DishService;
import com.wing.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;

    /**
     * 新增菜品和对应的口味
     *
     * @param dishDTO
     */
    @Override
    @Transactional//由于涉及到多数据表的操作所以需要采用事务的形式来保证操作的原子型
    public void saveWithFlavor(DishDTO dishDTO) {

        //创建菜品实体类对象
        Dish dish = new Dish();

        //进行属性拷贝
        BeanUtils.copyProperties(dishDTO, dish);

        //向菜品表插入数据，一次一条
        dishMapper.insert(dish);

        //获取insert语句生成的主键值
        Long dishId = dish.getId();

        //向口味表插入n条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {//口味表不为空

            //遍历集合，给每种口味都对应上菜品的id
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });

            dishFlavorMapper.insertFlavors(flavors);
        }

    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {

        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //下一条sql进行分页，自动加入limit关键字分页
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());

    }

    /**
     * 菜品批量删除
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {

        //判断当前菜品是否能够删除
        //1、菜品是否处于“起售”状态
        //根据id查询菜品
        for (Long id : ids) {
            //获取到一条菜品数据
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {//菜品处于起售状态
                //向前端抛出异常
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //2、菜品是否关联了套餐
        List<Long> setMealDishIds = setMealDishMapper.getSetMealDishIds(ids);
        if (setMealDishIds != null && setMealDishIds.size() > 0) {//当前菜品被套餐关联
            //向前端抛出异常
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品数据
//        for (Long id : ids) {
//            dishMapper.deleteById(id);
//            //删除菜品关联的口味数据
//            dishFlavorMapper.deleteById(id);
//        }减少发送sql语句的次数

        //根据获取到的list直接批量删除菜品
        dishMapper.deleteByIds(ids);

        //根据获取到的list直接批量删除口味
        dishFlavorMapper.deleteByDishIds(ids);
    }

    /**
     * 根据id查询菜品和对应的口味
     *
     * @return
     */
    @Override
    public DishVO getByIdWithFlavor(Long id) {

        //根据id查询菜品
        Dish dish = dishMapper.getById(id);

        //根据id查询菜品对应的口味
        List<DishFlavor> dishFlavors = dishFlavorMapper.getFlavorByDishId(id);

        //将查询到的数据封装到dishVO当中
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);

        return dishVO;
    }

    /**
     * 根据id修改菜品信息和对应的口味
     *
     * @param dishDTO
     */
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {

        Dish dish = new Dish();
        Long dishId = dishDTO.getId();
        BeanUtils.copyProperties(dishDTO, dish);

        //修改菜品表的信息
        dishMapper.update(dish);

        //删除原有的口味数据
        dishFlavorMapper.deleteByDishId(dishId);

        //新增口味数据信息
        List<DishFlavor> newDishFlavors = dishDTO.getFlavors();
        if (newDishFlavors != null && newDishFlavors.size() > 0) {//口味表不为空

            //遍历集合，给每种口味都对应上菜品的id
            newDishFlavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });

            dishFlavorMapper.insertFlavors(newDishFlavors);
        }


    }

    /**
     * 起售或停售菜品
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStopSale(Integer status, Long id) {

        //创建dish实体对象
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();

        dishMapper.update(dish);
    }


}
